package com.freight.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.freight.common.core.constant.DictConstants;
import com.freight.tws.api.domain.Vehicle;
import lombok.RequiredArgsConstructor;
import com.freight.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.freight.common.log.annotation.Log;
import com.freight.common.log.enums.BusinessType;
import com.freight.common.core.web.controller.BaseController;
import com.freight.common.core.web.domain.AjaxResult;
import com.freight.common.core.utils.poi.ExcelUtil;
import com.freight.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.web.page.Condition;
import com.freight.tws.service.IVehicleService;
import com.freight.common.core.web.page.TableDataInfo;

/**
 * 车辆信息Controller
 * 
 * @author ruoyi
 * @date 2022-01-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/vehicle")
public class VehicleController extends BaseController {
    private final IVehicleService iVehicleService;

    /**
     * 查询车辆信息列表
     */
    @PreAuthorize(hasPermi="tws:vehicle:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, Vehicle vehicle) {
        LambdaQueryWrapper<Vehicle> lqw = new LambdaQueryWrapper<Vehicle>();
        if (StringUtils.isNotBlank(vehicle.getType())) {
            lqw.eq(Vehicle::getType, vehicle.getType());
        }
        if (StringUtils.isNotBlank(vehicle.getVehicleNumber())) {
            lqw.like(Vehicle::getVehicleNumber, vehicle.getVehicleNumber());
        }
        if (StringUtils.isNotBlank(vehicle.getVehiclePlateColorCode())) {
            lqw.eq(Vehicle::getVehiclePlateColorCode, vehicle.getVehiclePlateColorCode());
        }
        if (StringUtils.isNotBlank(vehicle.getVehicleType())) {
            lqw.eq(Vehicle::getVehicleType, vehicle.getVehicleType());
        }
        if (StringUtils.isNotBlank(vehicle.getVehicleEnergyType())) {
            lqw.eq(Vehicle::getVehicleEnergyType, vehicle.getVehicleEnergyType());
        }
        if (vehicle.getVehicleTonnage() != null) {
            lqw.eq(Vehicle::getVehicleTonnage, vehicle.getVehicleTonnage());
        }
        if (vehicle.getGrossMass() != null) {
            lqw.eq(Vehicle::getGrossMass, vehicle.getGrossMass());
        }
        if (StringUtils.isNotBlank(vehicle.getRoadTransportCertificateNumber())) {
            lqw.eq(Vehicle::getRoadTransportCertificateNumber, vehicle.getRoadTransportCertificateNumber());
        }
        if (StringUtils.isNotBlank(vehicle.getShortCode())) {
            lqw.eq(Vehicle::getShortCode, vehicle.getShortCode());
        }
        if (vehicle.getDriverId() != null) {
            lqw.eq(Vehicle::getDriverId, vehicle.getDriverId());
        }
        if (StringUtils.isNotBlank(vehicle.getOwner())) {
            lqw.eq(Vehicle::getOwner, vehicle.getOwner());
        }
        if (StringUtils.isNotBlank(vehicle.getUseCharacter())) {
            lqw.eq(Vehicle::getUseCharacter ,vehicle.getUseCharacter());
        }
        if (StringUtils.isNotBlank(vehicle.getVin())) {
            lqw.eq(Vehicle::getVin, vehicle.getVin());
        }
        if (StringUtils.isNotBlank(vehicle.getIssuingOrganizations())) {
            lqw.eq(Vehicle::getIssuingOrganizations, vehicle.getIssuingOrganizations());
        }
        if (vehicle.getRegisterDate() != null) {
            lqw.eq(Vehicle::getRegisterDate, vehicle.getRegisterDate());
        }
        if (vehicle.getIssueDate() != null) {
            lqw.eq(Vehicle::getIssueDate, vehicle.getIssueDate());
        }
        if (vehicle.getIsCert() != null) {
            lqw.eq(Vehicle::getIsCert, vehicle.getIsCert());
        }
        if (vehicle.getState() != null) {
            lqw.eq(Vehicle::getState, vehicle.getState());
        }
        lqw.orderByDesc(Vehicle::getCreateTime);
        IPage<Vehicle> list = iVehicleService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 所有车辆
     */
//    @PreAuthorize(hasPermi="tws:vehicle:list")
//    @GetMapping("/listAll")
//    public AjaxResult listAll(Vehicle vehicle) {
//        LambdaQueryWrapper<Vehicle> lqw = new LambdaQueryWrapper<Vehicle>();
//        if (StringUtils.isNotBlank(vehicle.getVehicleNumber())) {
//            lqw.like(Vehicle::getVehicleNumber, vehicle.getVehicleNumber());
//        }
//        if (StringUtils.isNotBlank(vehicle.getVehiclePlateColorCode())) {
//            lqw.eq(Vehicle::getVehiclePlateColorCode, vehicle.getVehiclePlateColorCode());
//        }
//        if (StringUtils.isNotBlank(vehicle.getVehicleType())) {
//            lqw.eq(Vehicle::getVehicleType, vehicle.getVehicleType());
//        }
//        if (StringUtils.isNotBlank(vehicle.getVehicleEnergyType())) {
//            lqw.eq(Vehicle::getVehicleEnergyType, vehicle.getVehicleEnergyType());
//        }
//        if (vehicle.getVehicleTonnage() != null) {
//            lqw.eq(Vehicle::getVehicleTonnage, vehicle.getVehicleTonnage());
//        }
//        if (vehicle.getGrossMass() != null) {
//            lqw.eq(Vehicle::getGrossMass, vehicle.getGrossMass());
//        }
//        if (StringUtils.isNotBlank(vehicle.getRoadTransportCertificateNumber())) {
//            lqw.eq(Vehicle::getRoadTransportCertificateNumber, vehicle.getRoadTransportCertificateNumber());
//        }
//        if (StringUtils.isNotBlank(vehicle.getShortCode())) {
//            lqw.eq(Vehicle::getShortCode, vehicle.getShortCode());
//        }
//        if (vehicle.getDriverId() != null) {
//            lqw.eq(Vehicle::getDriverId, vehicle.getDriverId());
//        }
//        if (StringUtils.isNotBlank(vehicle.getOwner())) {
//            lqw.eq(Vehicle::getOwner, vehicle.getOwner());
//        }
//        if (StringUtils.isNotBlank(vehicle.getUseCharacter())) {
//            lqw.eq(Vehicle::getUseCharacter ,vehicle.getUseCharacter());
//        }
//        if (StringUtils.isNotBlank(vehicle.getVin())) {
//            lqw.eq(Vehicle::getVin, vehicle.getVin());
//        }
//        if (StringUtils.isNotBlank(vehicle.getIssuingOrganizations())) {
//            lqw.eq(Vehicle::getIssuingOrganizations, vehicle.getIssuingOrganizations());
//        }
//        if (vehicle.getRegisterDate() != null) {
//            lqw.eq(Vehicle::getRegisterDate, vehicle.getRegisterDate());
//        }
//        if (vehicle.getIssueDate() != null) {
//            lqw.eq(Vehicle::getIssueDate, vehicle.getIssueDate());
//        }
//        if (vehicle.getIsCert() != null) {
//            lqw.eq(Vehicle::getIsCert, vehicle.getIsCert());
//        }
//        if (vehicle.getState() != null) {
//            lqw.eq(Vehicle::getState, vehicle.getState());
//        }
//        lqw.orderByDesc(Vehicle::getCreateTime);
//        List<Vehicle> list = iVehicleService.list(lqw);
//        return AjaxResult.success(list);
//    }

    /**
     * 审核中计数
     */
    @GetMapping("/getCertNum")
    public AjaxResult getCertNum() {
        LambdaQueryWrapper<Vehicle> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Vehicle::getIsCert, DictConstants.IS_CERT.SHZ.getKey());
        int count = iVehicleService.count(lqw);
        return AjaxResult.success(count);
    }

    /**
     * 导出车辆信息列表
     */
    @PreAuthorize(hasPermi = "tws:vehicle:export")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Vehicle vehicle) throws IOException
    {
        LambdaQueryWrapper<Vehicle> lqw = new LambdaQueryWrapper<Vehicle>(vehicle);
        List<Vehicle> list = iVehicleService.list(lqw);
        ExcelUtil<Vehicle> util = new ExcelUtil<Vehicle>(Vehicle. class);
        util.exportExcel(response, list, "vehicle");
    }

    /**
     * 获取车辆信息详细信息
     */
    @PreAuthorize(hasPermi="tws:vehicle:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iVehicleService.getById(id));
    }

    /**
     * 新增车辆信息
     */
    @PreAuthorize(hasPermi="tws:vehicle:add")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Vehicle vehicle) {
        return toAjax(iVehicleService.save(vehicle) ? 1 : 0);
    }

    /**
     * 修改车辆信息
     */
    @PreAuthorize(hasPermi="tws:vehicle:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Vehicle vehicle) {
        return toAjax(iVehicleService.updateById(vehicle) ? 1 : 0);
    }

    /**
     * 车辆审核
     */
    @PreAuthorize(hasPermi="tws:vehicle:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PostMapping("/cert")
    public AjaxResult cert(@RequestBody Vehicle vehicle) {
        return toAjax(iVehicleService.cert(vehicle));
    }

    /**
     * 车辆禁/启用
     */
    @PreAuthorize(hasPermi="tws:vehicle:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PostMapping("/stateChange")
    public AjaxResult stateChange(@RequestBody Vehicle vehicle) {
        return toAjax(iVehicleService.stateChange(vehicle));
    }

    /**
     * 删除车辆信息
     */
    @PreAuthorize(hasPermi="tws:vehicle:remove")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iVehicleService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 上传车辆
     */
    @PreAuthorize(hasPermi="tws:vehicle:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @GetMapping("/upload/{id}")
    public AjaxResult upload(@PathVariable Long id) {
        return toAjax(iVehicleService.upload(id));
    }

}
